package com.xiaotu.spider.maoyan.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.*;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @类名 MaoYanFilmSoonPageProcessor
 * @日期 2017年7月31日
 * @作者 高海军
 * @功能 猫眼即将上映影片爬取
 */
@Component("MaoYanFilmSoonPageProcessor")
public class MaoYanFilmSoonPageProcessor extends AUrlPageProcessor
{
    public static final String LIST_URL = "http://maoyan.com/films?showType=2&sortId=1";

    private static final String[] OCCP_NAMES = new String[]{"导演", "演员", "编剧", "监制", "原著作者"};

    @SuppressWarnings("unchecked")
    @Override
    public void process(Page page)
    {
        if (Constants.PageDataType.TV_RECORD.equals(this.getPageDataType(page)))
            this.listProcess(page);
        else
            this.detailProcess(page);
    }

    private void listProcess(Page page)
    {
        List<String> linkList = page.getHtml().xpath
                ("//dl[@class='movie-list']/dd//div[@class='movie-item-title']/a/@href").all();
        if (linkList == null || linkList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        linkList.forEach(link -> PageUtils.addTargetRequest(page, link, page.getRequest()
                .getExtras()));

        List<String> tagList = page.getHtml().xpath("//ul[@class='list-pager']//a/text()").all();
        if (tagList == null || tagList.isEmpty())
            return;
        if ("下一页".equals(tagList.get(tagList.size() - 1)))
        {
            List<String> pageList = page.getHtml().xpath("//ul[@class='list-pager']//a/@href").all();
            PageUtils.addTargetRequest(page, pageList.get(pageList.size() - 1), page.getRequest()
                    .getExtras());
        }
    }

    private void detailProcess(Page page)
    {
        Html html = page.getHtml();
        String name = html.xpath("//h3[@class='name']/text()").get();
        if (StringUtils.isEmpty(name))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        String engName = html.xpath("//div[@class='ename']/text()").get();
        List<String> liList = html.xpath("//li[@class='ellipsis']/text()").all();
        if (liList == null || liList.size() < 3)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<String> divList = html.xpath("//div[@class='celebrity-group']").all();
//        if (divList == null || divList.isEmpty())
//        	throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        String desc = html.xpath("//div[@class='mod-content']/span/text()").get();

        List<String[]> starList = new ArrayList<>();
        IntStream.range(0, OCCP_NAMES.length).forEach(i -> starList.add(this.getStars(divList
                .stream().filter(divStr -> divStr.contains(OCCP_NAMES[i])).findFirst().orElse
                        (null))));

        String area = liList.get(1).split("/")[0].trim();
        String playDate = RegexUtils.regexSearch(RegexUtils.REGEX_DATE, liList.get(2));
        playDate = playDate == null ? RegexUtils.regexSearch("[0-9]{4}-[0-9]{2}", liList.get(2))
                : playDate;
        playDate = playDate == null ? RegexUtils.regexSearch("[0-9]{4}", liList.get(2)) : playDate;
        String playArea = playDate == null ? null : liList.get(2).replace(playDate, StringUtils
                .EMPTY);
        page.putField(MovieSoonModel.class.getName(), new MovieSoonModel(name, engName, liList
                .get(0), area, playDate, playArea, null, starList.get(0), starList.get(1),
                starList.get(2), starList.get(3), starList.get(4), desc, page.getUrl().toString()));
    }

    private String[] getStars(String divStr)
    {
        if (StringUtils.isEmpty(divStr))
            return null;
        Html html = new Html(divStr);
        List<String> nameList = html.xpath("//a[@class='name']/text()").replace(" ", StringUtils
                .EMPTY).all();
        if (nameList == null || nameList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        return nameList.toArray(new String[0]);
    }


    @Override
    protected String[] getStartRequestUrls()
    {
        return new String[]{LIST_URL};
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.MOVIE_SOON;
    }

    @Override
    public String getPageDataType(Page page)
    {
        if (page.getUrl().toString().contains("showType=2"))
            return Constants.PageDataType.TV_RECORD;
        return Constants.PageDataType.SEARCH_LIST;
    }

}
